package cn.com.xiaoyaoji.utils;

import org.dom4j.Element;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CXUArg {

	private String name;
	
	private String type;
	
	private String require;
	
	private String description;
	
	private String defaultValue;
	
	private String testValue;
	
	private List<CXUArg> children = new ArrayList<>();
	
	public interface Require {
		
		public static String TRUE = "true";
		public static String FALSE = "false";
		
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getRequire() {
		return require;
	}

	public void setRequire(String require) {
		this.require = require;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}

	public String getTestValue() {
		return testValue;
	}

	public void setTestValue(String testValue) {
		this.testValue = testValue;
	}

	public List<CXUArg> getChildren() {
		return children;
	}

	public void setChildren(List<CXUArg> children) {
		this.children = children;
	}

	public static CXUArg builder(Element element) throws InstantiationException, IllegalAccessException {
		Class<CXUArg> cls = CXUArg.class;
		CXUArg arg = cls.newInstance();
		arg.setName(element.getName());
		String req = element.attributeValue("occurrence");
		arg.setRequire("1".equals(req) ? Require.TRUE : Require.FALSE);
		arg.setType(element.attributeValue("type")==null?"":element.attributeValue("type").toLowerCase());
		arg.setDescription(element.attributeValue("desc")==null?"":element.attributeValue("desc"));
		arg.setDefaultValue(element.getText()==""?element.attributeValue("value"):element.getText());
		if (arg.getDefaultValue() == null){
			arg.setDefaultValue("");
		}
		arg.setTestValue("");
		
		@SuppressWarnings("unchecked")
		List<Element> elements = element.elements();
		if( elements!= null && !elements.isEmpty()) {
			arg.setChildren(multibuilder(elements));
		}
		return arg;
	}
	
	public static List<CXUArg> multibuilder(List<Element> element) throws InstantiationException, IllegalAccessException {
		if (element == null){
			return new ArrayList<>();
		}
		List<CXUArg> cxuArg = new ArrayList<>();
		for(Element ele: element) {
			cxuArg.add(CXUArg.builder(ele));
		}
		return cxuArg;
	}

	public static CXUArg dbuilder(String key, String value) throws InstantiationException, IllegalAccessException {
		Class<CXUArg> cls = CXUArg.class;
		CXUArg arg = cls.newInstance();
		String name = key;
		String defaultValue = value;
		arg.setName(name);
		arg.setRequire("true");
		arg.setType("string");
		arg.setDescription(null);
		arg.setDefaultValue(defaultValue);
		if (arg.getDefaultValue() == null){
			arg.setDefaultValue("");
		}
		arg.setTestValue("");

		return arg;
	}


	public static List<CXUArg> dsfbuilder(File file,String modelName) throws InstantiationException, IllegalAccessException {
		Class<CXUArg> cls = CXUArg.class;
		CXUArg arg = cls.newInstance();
		List<CXUArg> cxuArg = new ArrayList<>();
		String FName = file.getName();
		String[] a = FName.split("_");
		String serviceName = a[0];
		String methodName = a[1];
		String[] c = methodName.split("[.]");
		methodName = c[0];
		Map<String,String> m1 =new HashMap<String,String>();
		m1.put("modelName", modelName);
		m1.put("serviceName", serviceName);
		m1.put("methodName", methodName);
		StringBuilder result = new StringBuilder();
		try{
            InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "GB2312");//防止中文乱码
            BufferedReader br = new BufferedReader(isr);//构造一个BufferedReader类来读取文件
            String s = null;
            while((s = br.readLine())!=null){//使用readLine方法，一次读一行
                result.append(System.lineSeparator()+s);
            }
			br.close();

		    m1.put("jsonMessage", result.toString());

		}catch(Exception e){
			e.printStackTrace();
		}
		for(String key:m1.keySet()){
			String value = m1.get(key).toString();
			String keys = key;
			cxuArg.add(CXUArg.dbuilder(keys,value));
		}
		return cxuArg;
	}
}
